//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.tuiofx.internal.gesture;

import java.util.HashMap;
import java.util.Map;

import javafx.application.Platform;
import javafx.event.EventTarget;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import org.tuiofx.internal.base.TuioFXCanvas;
import org.tuiofx.internal.util.LogUtils;

class CursorTracker {
    private Map<Integer, Circle> activeTrackers;
    private Scene scene;
    Map<EventTarget, Circle> centroidByNode;
    private Circle centroid;
    private TuioFXCanvas tuioFXCanvas;

    public CursorTracker(Scene scene) {
        this.scene = scene;
        this.activeTrackers = new HashMap<>();
        this.centroidByNode = new HashMap<>();
    }

    public void addTouchPoint(final int pointId, final double sceneX, final double sceneY) {
        Platform.runLater(new Runnable() {
            public void run() {
                CursorTracker.this.addTracker(pointId, sceneX, sceneY);
            }
        });
    }

    public void updateCentroid(final Point2D centroidPoint) {
        Platform.runLater(new Runnable() {
            public void run() {
                Point2D point = CursorTracker.this.getSceneXYRelativeToRoot(centroidPoint.getX(), centroidPoint.getY());
                if (CursorTracker.this.centroid == null) {
                    CursorTracker.this.centroid = new Circle(point.getX(), point.getY(), 5.0);
                    CursorTracker.this.centroid.setFill(Color.RED);
                    CursorTracker.this.centroid.setMouseTransparent(true);
                    CursorTracker.this.addNodeToRoot(CursorTracker.this.centroid);
                } else {
                    CursorTracker.this.centroid.setCenterX(point.getX());
                    CursorTracker.this.centroid.setCenterY(point.getY());
                    CursorTracker.this.centroid.setVisible(true);
                }

            }
        });
    }

    public void removeCentroid() {
        Platform.runLater(new Runnable() {
            public void run() {
                if (CursorTracker.this.centroid != null) {
                    CursorTracker.this.centroid.setVisible(false);
                }
            }
        });
    }

    public void addNodeToRoot(Node node) {
        if (this.getTuioFXCanvas() != null) {
            this.getTuioFXCanvas().getChildren().add(node);
        }

    }

    private TuioFXCanvas getTuioFXCanvas() {
        if (this.tuioFXCanvas == null) {
            if (this.scene.getRoot() instanceof TuioFXCanvas) {
                this.tuioFXCanvas = (TuioFXCanvas) this.scene.getRoot();
            } else {
                LogUtils.error("Root node is not from type TuioFXCanvas");
            }
        }

        return this.tuioFXCanvas;
    }

    private void addTracker(int pointId, double sceneX, double sceneY) {
        Circle tracker = this.createTracker(pointId, sceneX, sceneY);
        this.activeTrackers.put(pointId, tracker);
    }

    private Circle createTracker(int pointId, double sceneX, double sceneY) {
        Point2D point = this.getSceneXYRelativeToRoot(sceneX, sceneY);
        Circle tracker = new Circle(point.getX(), point.getY(), 7.0);
        tracker.setId("TouchPoint-" + pointId);
        tracker.setStroke(Color.BLUE);
        tracker.setFill((Paint) null);
        tracker.setMouseTransparent(true);
        this.addNodeToRoot(tracker);
        return tracker;
    }

    public void updateTouchPoint(int pointId, double sceneX, double sceneY) {
        Platform.runLater(() -> {
            this.updateTracker(pointId, sceneX, sceneY);
        });
    }

    private void updateTracker(int pointId, double sceneX, double sceneY) {
        Circle tracker = (Circle) this.activeTrackers.get(pointId);
        if (tracker != null) {
            Point2D point = this.getSceneXYRelativeToRoot(sceneX, sceneY);
            tracker.setCenterX(point.getX());
            tracker.setCenterY(point.getY());
        }
    }

    public void removeTouchPoint(int pointId) {
        Platform.runLater(() -> {
            this.removeTracker(pointId);
        });
    }

    private void removeTracker(int pointId) {
        Circle tracker = (Circle) this.activeTrackers.get(pointId);
        if (tracker != null) {
            if (this.getTuioFXCanvas() instanceof TuioFXCanvas) {
                this.getTuioFXCanvas().getChildren().remove(tracker);
            } else {
                LogUtils.error("Couldn't remove cursor tracker");
            }

        }
    }

    private Point2D getSceneXYRelativeToRoot(double sceneX, double sceneY) {
        if (this.getTuioFXCanvas() == null) {
            return new Point2D(0.0, 0.0);
        } else {
            double x = sceneX + -this.getTuioFXCanvas().getTranslateX();
            double y = sceneY + -this.getTuioFXCanvas().getTranslateY();
            return new Point2D(x, y);
        }
    }
}
